package com.shop.controller;

import com.shop.constant.MessageConstant;
import com.shop.entity.Result;
import com.shop.pojo.Shopcart;
import com.shop.service.ShopcartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shopcart")
@Api(tags = "购物车管理")
public class ShopcartController {
    @Reference
    private ShopcartService shopcartService;

    @PostMapping("/add")
    @ApiOperation("添加购物车商品接口")
    private Result add(@RequestBody Shopcart shopcart){
        Boolean b = shopcartService.sameGoods(shopcart);
        if (!b){
            shopcartService.add(shopcart);
            return new Result(true, MessageConstant.ADD_SUCCESS);
        }else{
            return new Result(false,MessageConstant.ADD_FAIL);
        }
    }

    @GetMapping("/findAll")
    @ApiOperation("展示用户购物车商品接口")
    @ApiImplicitParams({@ApiImplicitParam(dataType = "int",name = "uid",value = "用户id",required = true,example = "0")})
    private Result findAll(@RequestParam(required = true) Integer uid){
        List<Shopcart> list=shopcartService.findAll(uid);
        return new Result(true,MessageConstant.QUERY_SUCCESS,list);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户购物车商品接口")
    @ApiImplicitParams({@ApiImplicitParam(dataType = "int",name = "uid",value = "用户id",required = true,example = "0"),@ApiImplicitParam(dataType = "int",name = "gid",value = "商品id",required = true,example = "0")})
    private Result delete(@RequestParam(required = true) Integer uid,@RequestParam(required = true) Integer gid){
        shopcartService.delete(uid,gid);
        return new Result(true,MessageConstant.DELETE_SUCCESS);
    }

    @GetMapping("/getcheckGoods")
    @ApiOperation("用户选择商品接口")
    @ApiImplicitParams({@ApiImplicitParam(dataType = "string",name = "gids",value = "选择的商品id",required = true,example = "0")})
    private Result getCheckGoods(String gids,Integer uid){
        List<Shopcart> list=shopcartService.findCheckGoods(gids,uid);
        return new Result(true,MessageConstant.QUERY_SUCCESS,list);
    }
}
